//
//  ThemeButton.m
//  Custom_Weibo
//
//  Created by LEO on 12/9/15.
//  Copyright © 2015 Leo_Lei. All rights reserved.
//

#import "ThemeButton.h"

@implementation ThemeButton

#pragma mark - INITIALIZATION_METHODS
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self getNotification];
    }
    return self;
}

- (void)awakeFromNib {
    [self getNotification];
}

#pragma mark - MONITOR_NOTIFICATION
- (void)getNotification {
    //  Monitoring the notification when theme changed.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(themeDidChange:)
                                                 name:kThemeNotification
                                               object:nil];
}

#pragma mark - WHEN_THEME_CHANGED
- (void)themeDidChange:(NSNotification *)notification {
    [self loadImage];
}

#pragma mark NORMAL_BUTTON_IMAGE
- (void)setDefalutImageName:(NSString *)defalutImageName {
    //  Get the object and set target image for its normal status.
    if (_defalutImageName != defalutImageName) {
        _defalutImageName = [defalutImageName copy];
        [self loadImage];
    }
}

#pragma mark HIGHLIGHTED_BUTTON_IMAGE
- (void)setHighlightedImageName:(NSString *)highlightedImageName {
    //  Get the object and set target image for its highlighted status.
    if (_highlightedImageName != highlightedImageName) {
        _highlightedImageName = [highlightedImageName copy];
        [self loadImage];
    }
}

#pragma mark - LOAD_IMAGE
- (void)loadImage {
    ThemeManager *manager = [ThemeManager defaultManager];
    
    //  Load picture in normal and highlighted.
    UIImage *image = [manager getThemeImage:_defalutImageName];
    UIImage *highImage = [manager getThemeImage:_highlightedImageName];
    
    [self setImage:image forState:UIControlStateNormal];
    [self setImage:highImage forState:UIControlStateHighlighted];
}

#pragma mark - DEALLOC
- (void)dealloc {
    //  Remove observer
    [[NSNotificationCenter defaultCenter] removeObserver:kThemeNotification];
}
@end
